import moment from "moment";
import * as ddscode from "dingtalk-jsapi";
let app = getApp();

Page({
  data: {
    currentContentId: 0,   //默认展示第一个未完成的内容
    purchaseOrderList: [],
    slotModalShow: false,
    slotButtonList: [
      {
        name: '关闭',
      }
    ],

    StatusOptions: [{
      value: 0,
      name: '交通'
    },
    {
      value: 1,
      name: '餐饮'
    }],
    statusindex: '',
    startData: '',  //时间相关
    endData: '',

    activeIndex: 0,
    // checked: true,

    page:1,
    page_size:20,
    searchInput:"",
    dowm:true,
    showEndTip: false, // 控制是否显示到底提示
  },

  onLoad() {
    var that = this;
    var date = new Date();
    that.setData({
      purchaseOrderList: [],
    })
    setTimeout(() => {
      this.setData({
        activeIndexDefault: 1
      })
    }, 2000)

    that.getPurchaseOrderList('')

  },

  onShow() {
    var that = this;
    // that.setData({
    //   purchaseOrderList: [],
    // })
    // that.getPurchaseOrderList()
    
  },

    // 扫码
    onSearchShop() {
      const that = this;
      dd.scan({
          type: 'qr',
          success: (res) => {
              console.log("扫码结果", res.code);
              that.onSearchInput({ detail: { value: res.code } }); // 调用输入框值改变事件，并传入扫码结果
          },
          fail: (err) => {
              // 扫码失败后的回调函数，可以根据实际需求进行处理
          }
      });
  },


// 点击了哪个状态
  toApprovalHall(e) {
    const contentId = e.currentTarget.dataset.id; // 获取点击的内容的id
    console.log("我点击了第几个=====>",contentId);
    var that = this;
    that.setData({
      currentContentId: contentId, // 更新当前展示的内容id
      page:1,
      purchaseOrderList:[],
      dowm:true,
    });

    if(contentId <= 1){
      that.getPurchaseOrderList('');
    }

  },

  // 点击查看详情跳转
  goprojectFollow(e){
    var item = e.currentTarget.dataset.item; 
    console.log("item==>",item)
    item.报销截图 = item.报销截图.replace(/\&/g, "|");
    dd.navigateTo({
      url: '/pages/projectFollow/projectFollow?item=' + JSON.stringify(item) +"&id=2",
    });
  },

  //状态下拉框 状态改变可以拿到下标 赋值 还可以根据下标知道选中的是哪个值 
  statusChange(e) {
    this.setData({
      statusindex: e.detail.value,
      status: e.detail.value
    })
  },

  // 获取输入内容进行搜索
  onSearchInput(e){
    console.log("e===>>",e)
    var that = this;
    that.setData({
      searchInput:e,
      page:1,
      dowm:true,
      purchaseOrderList:[]
    })
    
    that.getPurchaseOrderList();

  },

  // 用于监听输入框值改变事件
onSearchInput(e) {
  var that = this;
  var searchTerm = e.detail.value.trim(); // 获取输入框的搜索内容并去除首尾空格
  console.log("输入框用来搜索的内容", searchTerm);

  // 清除之前的搜索定时器
  clearTimeout(that.data.searchTimer);

  // 设置一个新的定时器，在用户停止输入300毫秒后执行搜索操作
  var searchTimer = setTimeout(function() {
    that.setData({
      searchTimer: null // 清空定时器
    });
    that.getPurchaseOrderList(searchTerm); // 执行搜索操作
  }, 300);

  // 将定时器存储在数据中
  that.setData({
    searchTimer: searchTimer
  });
},

// 清除搜索框内容
onSearchClear(e) {
  var that = this;
  that.setData({
    searchInput: '', // 清空搜索框内容
    page: 1,
    dowm: true,
    purchaseOrderList: []
  });
},


  // 获取配货单未完成数据
  getPurchaseOrderList(searchTerm) {
    var that = this;
     // 在请求开始前先清空数组
     that.setData({
      purchaseOrderList: []  // 清空数组
    });

    var currentContentId = that.data.currentContentId
    var bx_status = 0;
    if(currentContentId == 0){
      bx_status = 0
    }else if(currentContentId == 1){
      bx_status =1
    }

    // select T1.FID AS FID,T1.F_ZYKJ_Text AS 订单号,T1.FBILLNO AS 单据编码,t1.F_ZYKJ_DATE  as 业务日期,T1.F_ZYKJ_Base_apv AS 客户ID,t3.FNAME as 客户名称,t1.F_ZYKJ_Base_tzk1 as 销售员ID,t4.fname AS 销售员,count(t2.F_ZYKJ_BASE1) AS 总款数,sum(t2.F_ZYKJ_BASEQTY )as 总数量,sum(t2.F_ZYKJ_BASEQTY-t2.F_ZYKJ_QTY_RE5) as 未配数量,T2.F_ZYKJ_QTY_QTR1 AS 可配数量,t2.F_ZYKJ_QTY_RE5 as 已配数量 from ZYKJ_SCRWD T1 left JOIN ZYKJ_SCRWD_Entry T2 ON T2.FID =T1.FID LEFT JOIN T_BD_CUSTOMER_L T3 ON T3.FCUSTID =T1.F_ZYKJ_Base_apv left join T_BD_OPERATORENTRY_L t4 on t4.FENTRYID=t1.F_ZYKJ_Base_tzk1 where t1.FDocumentStatus ='C' and T2.F_ZYKJ_QTY_QTR1 <> '0' and t2.F_ZYKJ_BillStatus_apv='0' and t2.F_ZYKJ_QTY_QTR1-t2.F_ZYKJ_QTY_RE5 > '0'  and t1.FBILLNO LIKE '%" + searchTerm + "%'  GROUP BY T1.FID,T1.F_ZYKJ_Text,T1.FBILLNO,t1.F_ZYKJ_DATE ,T1.F_ZYKJ_Base_apv ,t3.FNAME ,t1.F_ZYKJ_Base_tzk1,t4.fname ,T2.F_ZYKJ_QTY_QTR1,t2.F_ZYKJ_QTY_RE5 ORDER BY t1.FID

    var shujuKu = app.globaldata.cipher_code
    console.log("数据库==>",shujuKu);

    var data;

    if (bx_status == 0) {
      console.log("走0");
      var data = JSON.stringify({
        "q": {
          "encrypted_text": shujuKu,
          "key": "66249c7f42e1e400",
          "sql":"select T1.FID AS FID,T1.F_ZYKJ_Text AS 订单号,T1.FBILLNO AS 单据编码,t1.F_ZYKJ_DATE  as 业务日期,T1.F_ZYKJ_Base_apv AS 客户ID,t3.FNUMBER as 客户名称,t1.F_ZYKJ_Base_tzk1 as 销售员ID,t4.fname AS 销售员,count(t2.F_ZYKJ_BASE1) AS 总款数,sum(t2.F_ZYKJ_BASEQTY )as 总数量,sum(t2.F_ZYKJ_BASEQTY-t2.F_ZYKJ_QTY_RE5) as 未配数量,T2.F_ZYKJ_QTY_QTR1 AS 可配数量,t2.F_ZYKJ_QTY_RE5 as 已配数量 from ZYKJ_SCRWD T1 left JOIN ZYKJ_SCRWD_Entry T2 ON T2.FID =T1.FID LEFT JOIN T_BD_CUSTOMER T3 ON T3.FCUSTID =T1.F_ZYKJ_Base_apv left join T_BD_OPERATORENTRY_L t4 on t4.FENTRYID=t1.F_ZYKJ_Base_tzk1 where t1.FDocumentStatus ='C' and t2.F_ZYKJ_Qty_qtr1 > '0' and t1.FBillTypeId <> '662f0114a2b069' and t2.F_ZYKJ_BillStatus_apv='0'  and t1.FBILLNO LIKE '%" + searchTerm + "%'  GROUP BY T1.FID,T1.F_ZYKJ_Text,T1.FBILLNO,t1.F_ZYKJ_DATE ,T1.F_ZYKJ_Base_apv ,t3.FNUMBER ,t1.F_ZYKJ_Base_tzk1,t4.fname ,T2.F_ZYKJ_QTY_QTR1,t2.F_ZYKJ_QTY_RE5 ORDER BY t1.FID",
          "fields": [
            "FID","订单号","单据编码","业务日期","客户ID","客户名称","销售员ID","销售员","总款数","总数量","未配数量","可配数量","已配数量"
          ]
        },
        "p": {
          "page_number": 1,
          "page_size": 20
        }
      })
  
    } else if (bx_status == 1) {
      console.log("走1");
    data = JSON.stringify({
      "q": {
        "encrypted_text": shujuKu,
        "key": "66249c7f42e1e400",
        "sql": "select T1.FID AS FID,T1.F_ZYKJ_Text AS 订单号,T1.FBILLNO AS 单据编码,t1.F_ZYKJ_DATE  as 业务日期,T1.F_ZYKJ_Base_apv AS 客户ID,t3.FNUMBER as 客户名称,t1.F_ZYKJ_Base_tzk1 as 销售员ID,t4.fname AS 销售员,count(t2.F_ZYKJ_BASE1) AS 总款数,sum(t2.F_ZYKJ_BASEQTY )as 总数量,sum(t2.F_ZYKJ_BASEQTY-t2.F_ZYKJ_QTY_RE5) as 未配数量,T2.F_ZYKJ_QTY_QTR1 AS 可配数量,t2.F_ZYKJ_QTY_RE5 as 已配数量 from ZYKJ_SCRWD T1 left JOIN ZYKJ_SCRWD_Entry T2 ON T2.FID =T1.FID LEFT JOIN T_BD_CUSTOMER T3 ON T3.FCUSTID =T1.F_ZYKJ_Base_apv left join T_BD_OPERATORENTRY_L t4 on t4.FENTRYID=t1.F_ZYKJ_Base_tzk1 where t1.FDocumentStatus ='C'  and t2.F_ZYKJ_BillStatus_apv='1' and t2.F_ZYKJ_QTY_QTR1 = t2.F_ZYKJ_QTY_RE5 and t1.FBILLNO LIKE '%" + searchTerm + "%' GROUP BY T1.FID,T1.F_ZYKJ_Text,T1.FBILLNO,t1.F_ZYKJ_DATE ,T1.F_ZYKJ_Base_apv ,t3.FNUMBER ,t1.F_ZYKJ_Base_tzk1,t4.fname ,T2.F_ZYKJ_QTY_QTR1,t2.F_ZYKJ_QTY_RE5 ORDER BY t1.FID",
        "fields": [
          "FID","订单号","单据编码", "业务日期", "客户ID", "客户名称", "销售员ID", "销售员", "总款数", "总数量", "未配数量","可配数量","已配数量"
        ]
      },
      "p": {
        "page_number": 1,
        "page_size": 20
      }
    });
    }


    // var data = JSON.stringify({
    //   "q": {
    //     "encrypted_text": "eHzaxpESe1fCSMzPh8m8TQ==KP+e/IVJg5zArGsXfb7W7lKdYcP6COENF6u+cSFCQEY=",
    //     "key": "65af0c304f878e00",
    //     "sql": "select T1.FID AS FID,T1.FBILLNO AS 单据编码,t1.F_ZYKJ_DATE  as 业务日期,T1.F_ZYKJ_Base_apv AS 客户ID,t3.FNAME as 客户名称,t1.F_ZYKJ_Base_tzk1 as 销售员ID,t4.fname AS 销售员,count(t2.F_ZYKJ_BASE1) AS 总款数,sum(t2.F_ZYKJ_BASEQTY )as 总数量,sum(t2.F_ZYKJ_BASEQTY-t2.F_ZYKJ_QTY_RE5) as 未配数量,T2.F_ZYKJ_QTY_QTR1 AS 可配数量 from ZYKJ_SCRWD T1 left JOIN ZYKJ_SCRWD_Entry T2 ON T2.FID =T1.FID LEFT JOIN T_BD_CUSTOMER_L T3 ON T3.FCUSTID =T1.F_ZYKJ_Base_apv left join T_BD_OPERATORENTRY_L t4 on t4.FENTRYID=t1.F_ZYKJ_Base_tzk1 where t1.FDocumentStatus ='C' and T2.F_ZYKJ_QTY_QTR1 <> '0' and t2.F_ZYKJ_BILLSTATUS_APV='' and t1.FBILLNO LIKE '%" + searchTerm + "%' GROUP BY T1.FID,T1.FBILLNO,t1.F_ZYKJ_DATE ,T1.F_ZYKJ_Base_apv ,t3.FNAME ,t1.F_ZYKJ_Base_tzk1,t4.fname ,T2.F_ZYKJ_QTY_QTR1 ORDER BY t1.FID",
    //     "fields": [
    //       "FID","单据编码","业务日期","客户ID","客户名称","销售员ID","销售员","总款数","总数量","未配数量"
    //     ]
    //   },
    //   "p": {
    //     "page_number": 1,
    //     "page_size": 20
    //   }
    // })

    console.log("配货单数据查询",data);

    dd.httpRequest({
      url: 'http://cloud.reece-sewing.com:9351/king_sql_query',
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      data: data,
      dataType: 'json',
      success: function (res) {
        console.log('获取配货单未完成数据响应==>', res.data)
        if (res.data.code == 200) {
        console.log('获取配货单未完成数据响应200000==>', res.data)
          var list = res.data.data;
          var purchaseOrderList = that.data.purchaseOrderList;
          for(var i in list){
            purchaseOrderList.push(list[i])
          }
          if(list.length < that.data.page_size){
            that.setData({
              dowm:false
            })
          }
          that.setData({
            purchaseOrderList:purchaseOrderList
          })
          console.log("数据",purchaseOrderList);
        } else {
          console.log("配货未完成的无数据");
          // dd.showToast({
          //   type: 'fail',
          //   content: res.data.data,
          //   duration: 2000,
          // });
        }
      },
      fail: function (res) {
        // dd.alert({ content: 'fail' });
      },
    });
  },

    // 获取配货单已11111111111111111完成数据
    // getPurchaseOrderListEd(e) {
    //   var that = this;
    //   var currentContentId = that.data.currentContentId
    //   var bx_status = 0;
    //   if(currentContentId == 0){
    //     bx_status = 0
    //   }else if(currentContentId == 1){
    //     bx_status =1
    //   }
    //   var data = JSON.stringify({
    //     "q": {
    //       "encrypted_text": "eHzaxpESe1fCSMzPh8m8TQ==KP+e/IVJg5zArGsXfb7W7lKdYcP6COENF6u+cSFCQEY=",
    //       "key": "65af0c304f878e00",
    //       "sql": "select T1.FID AS FID,T1.FBILLNO AS 单据编码,t1.F_ZYKJ_DATE  as 业务日期,T1.F_ZYKJ_Base_apv AS 客户ID,t3.FNAME as 客户名称,t1.F_ZYKJ_Base_tzk1 as 销售员ID,t4.fname AS 销售员,count(t2.F_ZYKJ_Base3) AS 总款数,sum(t2.F_ZYKJ_BaseQty1 )as 总数量,sum(t2.F_ZYKJ_BaseQty1-t2.F_ZYKJ_Qty_83g) as 未拣数量 from ZYKJ_SCRWD T1 left JOIN F_SCRWD_Entry1 T2 ON T2.FID =T1.FID LEFT JOIN T_BD_CUSTOMER_L T3 ON T3.FCUSTID =T1.F_ZYKJ_Base_apv left join T_BD_OPERATORENTRY_L t4 on t4.FENTRYID=t1.F_ZYKJ_Base_tzk1 where t1.FDocumentStatus ='C'and t2.F_ZYKJ_BillStatus_re5='1' and t2.FBillStatus2='1' and t1.FBILLNO LIKE '%%' GROUP BY T1.FID,T1.FBILLNO,t1.F_ZYKJ_DATE ,T1.F_ZYKJ_Base_apv ,t3.FNAME ,t1.F_ZYKJ_Base_tzk1,t4.fname ORDER BY t1.FID",
    //       "fields": [
    //         "FID","单据编码","业务日期","客户ID","客户名称","销售员ID","销售员","总款数","总数量","未拣数量"
    //       ]
    //     },
    //     "p": {
    //       "page_number": 1,
    //       "page_size": 20
    //     }
    //   })
  
    //   dd.httpRequest({
    //     url: 'http://cloud.reece-sewing.com:9351/king_sql_query',
    //     method: 'POST',
    //     headers: {
    //       "Content-Type": "application/json"
    //     },
    //     data: data,
    //     dataType: 'json',
    //     success: function (res) {
    //       console.log('获取配货单未完成数据响应==>', res.data)
    //       if (res.data.code == 200) {
    //       console.log('获取配货单未完成数据响应200000==>', res.data)
    //         var list = res.data.data;
    //         var purchaseOrderList = that.data.purchaseOrderList;
    //         for(var i in list){
    //           purchaseOrderList.push(list[i])
    //         }
    //         if(list.length < that.data.page_size){
    //           that.setData({
    //             dowm:false
    //           })
    //         }
    //         that.setData({
    //           purchaseOrderList:purchaseOrderList
    //         })
    //         console.log("数据",purchaseOrderList);
    //       } else {
    //         console.log("配货未完成的无数据");
    //         // dd.showToast({
    //         //   type: 'fail',
    //         //   content: res.data.data,
    //         //   duration: 2000,
    //         // });
    //       }
    //     },
    //     fail: function (res) {
    //       // dd.alert({ content: 'fail' });
    //     },
    //   });
    // },




  getIndex(e) {
    console.log("type==>e", e)
    
  },



  onBlur(e) {
    // var that = this
    console.log('onBlur', e.detail)
    // var item = that.data.item
    // item.基本单位数量 =  e.detail
    // this.setData({
    //   item,
    // });
  },


// 点击下推配货单
  gopickingListTwo(e){
    console.log("e==>",e)
    var item = e.currentTarget.dataset.item
    dd.navigateTo({
      url: '/pages/pickingListTwo/pickingListTwo?item='+JSON.stringify(item),
      // url: '/pages/pickingListTwo/pickingListTwo?orderNumber=' + item['FID'] + '&danJubian=' + item['单据编码'] + '&yewuData=' + item['业务日期']

    })
  },

  gopickingListTwoy(e){
    console.log("e==>",e)
    var item = e.currentTarget.dataset.item
    console.log("已完成的item",item);
    dd.navigateTo({
      url: '/pages/pickingListTwoy/pickingListTwoy?item='+JSON.stringify(item),

    })
  },




    // // 点击下推调用接口并跳转 配货任务单下推配货单
    // gopickingListTwo(e) {
    //   // var that = this;
    //   console.log("e==>",e)

    //   var item = e.currentTarget.dataset.item
      
    //   console.log('点击的下推配货单item参数：', item);
    //   var data = JSON.stringify(
    //     {
    //       "formid": "ZYKJ_DDFJ",
    //       "RuleId": "1e3fba93-a02c-4753-9503-8af299836050",
    //       "EntryIds": [],
    //       "Ids": item['FID'],
    //       "TargetFormId": "ZYKJ_PHD",
    //       "FEntity": []
    //     }
    //   )
    //   // return
  
    //   dd.httpRequest({
    //     url: 'http://openapi.keyboardadmin.com:10003/reporting',
    //     method: 'POST',
    //     data: data,
    //     dataType: 'json',
    //     success: function (res) {
    //       if(res.data.code == 200){
    //         console.log('下推响应200==>',JSON.parse(res.data.data))
    //         var dataxiatui = JSON.parse(res.data.data)
    //         console.log("下推响应200解析后的数据ttttttttttt",dataxiatui);
    //         console.log("下推响应200解析的idddddddddddddddd====>",dataxiatui.Result.ResponseStatus.SuccessEntitys[0].Id);
    
    //         dd.confirm({
    //           title: '温馨提示',
    //           content: '是否确认查看',
    //           confirmButtonText: '确定查看',
    //           cancelButtonText: '暂不需要',
    //           success: (result) => {
    //             console.log(result);
    //             if (result.confirm) {
    //               dd.navigateTo({
    //                 // url: '/pages/login/login?stop=1'
    //                 // url: '/pages/pickingListTwo/pickingListTwo?id=' + dataxiatui.Result.ResponseStatus.SuccessEntitys[0].Id + '&orderNumber=' + item['订单号']+ '&gongYinag=' + item['供应商名称']
    //                 url: '/pages/pickingListTwo/pickingListTwo?id=' + dataxiatui.Result.ResponseStatus.SuccessEntitys[0].Id + '&orderNumber=' + item['FID'] + '&danJubian=' + item['单据编码'] + '&yewuData=' + item['业务日期']
    //               })
    //             }
        
    //           },
    //         });
    //       }else{
    //         console.log("错误");
    //       }
         
    //     },
    //     fail: function (res) {
    //       dd.alert({ content: 'fail' });
    //     },
    //     complete: function (res) {
    //       // dd.alert({ content: 'complete' });
    //       // my.hideLoading();
    //     }
    //   });
    // },









  onReachBottom(){
    var that = this;
    var page = that.data.page;
    var dowm = that.data.dowm;
    var currentContentId = that.data.currentContentId;
    if(dowm){
      that.setData({
        page:page+1
      })
  
      if(currentContentId < 2 ){
        this.getPurchaseOrderList();
      }else{
        this.getReimbursementList();
      }
      
    }
    
  },


});